﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using FineUI;
using SDLITS.Common;
using SDLITS.Model;
using MySoft.Data;
namespace SDLITS.Web.Basic
{
    public partial class EGUserChange : CommonBase
    {
        #region 页面初始化
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDept();
                LoadData();
            }
        }
        #endregion
        protected void BindDept()
        {
            List<Department> lists = SDDbSession.Default.From<Department>().ToList();
            drp_dept.DataSource = lists;
            drp_dept.DataTextField = "DeptName";
            drp_dept.DataValueField = "DeptID";
            drp_dept.DataBind();
        }
        #region 读取数据


        private void LoadData()
        {
            int id = 0;
            if (!string.IsNullOrEmpty(Request["id"]))
            {
                id = int.Parse(Request["id"]);
            }
            if (id > 0)
            {
                EGUser Tm = SDDbSession.Default.From<EGUser>().Where(EGUser._.EGUserID == id).ToFirst();
                txt_Name.Text = Tm.Name;
                txt_SN.Text = Tm.EGCardNo;
                drp_dept.SelectedValue = Tm.DeptID.ToString();
            }
        }
        #endregion

        #region 保存数据
        
        protected void btnSaveRefresh_Click(object sender, EventArgs e)
        {
            int returnvalue = 0;
            EGUser Tm = new EGUser();
            if (!string.IsNullOrEmpty(Request["id"]))
            {
                Tm.EGUserID = int.Parse(Request["id"]);
                Tm.Attach();
                Tm.Name = txt_Name.Text;
                Tm.EGCardNo = txt_SN.Text.Trim();
                Tm.EGCardNoDoor = Methed.Transfer(txt_SN.Text.Trim()).ToString();
                Tm.DeptID = int.Parse(drp_dept.SelectedValue);
                returnvalue = SDDbSession.Default.Update<EGUser>(Tm);
            }
            else
            {
                ModelLoginUser user = Session["LoginUser"] as ModelLoginUser;
                Tm.DeAttach();
                Tm.Name = txt_Name.Text;
                Tm.EGCardNo = txt_SN.Text.Trim();
                Tm.EGCardNoDoor = Methed.Transfer(txt_SN.Text.Trim()).ToString();
                Tm.DeptID = int.Parse(drp_dept.SelectedValue);
                Tm.CreateDate = DateTime.Now;
                Tm.CreateUserID = user.UserID;
                returnvalue = SDDbSession.Default.Insert<EGUser>(Tm);
            }
            if (returnvalue > 0)
            {
                PageContext.RegisterStartupScript(ActiveWindow.GetHideRefreshReference());
            }
        }
        
        #endregion
    }
}